package com.yida.wy.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.domain.R;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.bean.BeanUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.system.api.RemoteFileService;
import com.yida.system.api.domain.SysFile;
import com.yida.system.api.domain.UploadRequest;
import com.yida.wy.common.domain.WyUploadFile;
import com.yida.wy.common.service.IWyFileService;

/**
 * @author hmh
 * @date 2023/06/08
 */
@Service
public class WyFileServiceImpl implements IWyFileService {

    // 公维修使用桶名，后期改为配置获取
    private final String GWJ_BUCKETNAME = "eda-wy-file";

    @Autowired
    private RemoteFileService remoteFileService;

    /* 
     * 默认文件文件上传
     */
    @Override
    public WyUploadFile upload(MultipartFile file) {
        R<SysFile> resultSysFile = remoteFileService.uploadFileDynamicLink(file, GWJ_BUCKETNAME, null, false);
        checkStatus(resultSysFile);
        WyUploadFile guf = sysFileTransGwjUploadFile(resultSysFile.getData());
        guf.setFileType(getFileType(file));
        return guf;
    }

    /* 
     * 文件上传，自主选择是否动态地址返回
     */
    @Override
    public WyUploadFile uploadFileDynamicLink(MultipartFile file, String fileName, boolean dynamicLinkFlag) {
        R<SysFile> resultSysFile = remoteFileService.uploadFileDynamicLink(file, GWJ_BUCKETNAME, fileName, dynamicLinkFlag);
        checkStatus(resultSysFile);
        WyUploadFile guf = sysFileTransGwjUploadFile(resultSysFile.getData());
        guf.setFileType(getFileType(file));
        return guf;
    }

    /*
     *  获取文件信息
     */
    @Override
    public SysFile getFileInfo(String fileName, boolean dynamicLinkFlag) {
        UploadRequest ur = new UploadRequest();
        ur.setBucketName(GWJ_BUCKETNAME);
        ur.setFileName(fileName);
        ur.setDynamicLinkFlag(dynamicLinkFlag);
        R<SysFile> resultSysFile = remoteFileService.getFileInfo(ur);
        checkStatus(resultSysFile);
        return resultSysFile.getData();
    }

    /* 
     * 获取文件组信息     
     */
    @Override
    public List<SysFile> getFileInfoList(List<String> fileNames, boolean dynamicLinkFlag) {
        UploadRequest ur = new UploadRequest();
        ur.setBucketName(GWJ_BUCKETNAME);
        ur.setFileNames(fileNames);
        ur.setDynamicLinkFlag(dynamicLinkFlag);
        R<List<SysFile>> resultSysFile = remoteFileService.getFileInfoList(ur);
        checkStatus(resultSysFile);
        return resultSysFile.getData();
    }

    /* 
     * 根据文件名删除文件（需确保当前文件名在公维金全局唯一）
     */
    @Override
    public boolean delFile(String fileName) {
        UploadRequest ur = new UploadRequest();
        ur.setBucketName(GWJ_BUCKETNAME);
        ur.setFileName(fileName);
        R<Boolean> result = remoteFileService.delFile(ur);
        checkStatus(result);
        return result.getData();
    }

    private void checkStatus(R<?> r) {
        if (StringUtils.isNull(r) || StringUtils.isNull(r.getData())) {
            throw new ServiceException("GWJ远程调用文件操作失败！:" + r.getMsg());
        }
        if (R.FAIL == r.getCode()) {
            throw new ServiceException("GWJ远程调用文件操作失败！:" + r.getMsg());
        }
    }

    private String getFileType(MultipartFile file) {
        return FileTypeUtils.getExtension(file).toUpperCase();
    }

    private WyUploadFile sysFileTransGwjUploadFile(SysFile sysFile) {
        if (StringUtils.isNull(sysFile)) {
            throw new ServiceException("sysFile为空,无非完成转换！");
        }
        WyUploadFile g = new WyUploadFile();
        BeanUtils.copyBeanProp(g, sysFile);
        return g;
    }

    public static void main(String[] args) {
        SysFile sf = new SysFile();
        sf.setName("1");

        // GwjUploadFile g = (GwjUploadFile)sf;
        WyUploadFile g = new WyUploadFile();
        BeanUtils.copyBeanProp(g, sf);
        System.out.println(g);
    }

}
